import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import ssl
import time
import subprocess

def Mailer(to_list, th1=None, Subject=None, unipath=None):
    mail_host = 'smtp.qq.com'  # 邮箱服务器
    mail_user = '1941220150@qq.com'  # 发件人邮箱密码(当时申请smtp给的口令)
    mail_pwd = 'krmfhtzuxwdejgji'  # SMTP密码
    context = ssl.create_default_context()
    # 邮件内容
    mail = str(th1)
    msg = MIMEMultipart()
    msgtext = MIMEText(mail,_charset='utf8')
    # msgtext = MIMEText(mail.encode('utf8'), _subtype='html', _charset='utf8')
    msg['From'] = mail_user
    msg['Subject'] = Subject
    msg['To'] = ",".join(to_list)
    if unipath is not None:
        att1 = MIMEText(open(unipath, 'rb').read(), 'base64', 'gb2312')
        att1["Content-Type"] = 'application/octet-stream'
        att1.add_header('Content-Disposition', 'attachment', filename=(unipath))
        msg.attach(att1)
    msg.attach(msgtext)
    with smtplib.SMTP_SSL(mail_host, 465, context=context) as smtp:
        smtp.login(mail_user, mail_pwd)
        smtp.send_message(msg)


to_list = [
    # 多用户使用的list
    '1941220150@qq.com',
]

if __name__ == '__main__':
    result = subprocess.run(['ipconfig'], stdout=subprocess.PIPE)
    Mailer(to_list, th1=result.stdout.decode('gbk'), Subject='邮件标题1111'+str(result.returncode))
    # Mailer(to_list, th1=result.stdout.decode('gbk'), Subject='邮件标题'+str(result.returncode+i), unipath=r'Recyclable/Recyclable/recyclable1.jpg')